//
//  ReaderContentViewController.swift
//  SwiftStudy
//
//  Created by chang on 2018/3/17.
//  Copyright © 2018年 chang. All rights reserved.
//

import UIKit
import Alamofire
import SwiftyJSON

class ReaderContentViewController: UIViewController {

    var contentLabel : UILabel!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.defaultBackgroundColor
        self.navigationItem.title = "小说"
        
        contentLabel = UILabel.init(frame: CGRect.init(x: 0, y: self.navgationBarBottom, width: MAINSCREEN_WIDTH-24, height: MAINSCREEN_HEIGHT-self.navgationBarBottom))
        contentLabel.font = UIFont.systemFont(ofSize: 15)
        contentLabel.textColor = UIColor.defaultTextColor
        contentLabel.numberOfLines = 0
        self.view.addSubview(contentLabel)
        
        self.requestContentData()
    }

    func requestContentData() {
        let url = "http://app.robook.com/iosapi/chapterContent"
        let parameDic = ["chapterId":"1","novelId":"1594016","token":"28341995_08d54b58112ff11d33c5b8b4459a0aa1","versionCode":"177"]
        self.view.makeToastActivity(.center)
        Alamofire.request(url, method: .post, parameters: parameDic).responseJSON {
            [weak self] (response) in
            self?.view.hideToastActivity()
            if let responseCC = response.result.value {
                let json = JSON(responseCC)
                let content = json["content"].string
                self?.contentLabel.text = content
                
                /**这里初步想法是 可能需要解码处理，
                 但是一直解码失败，以后研究吧，changbo*/
                //解码
//                let data = NSData.init(base64Encoded: content!, options: NSData.Base64DecodingOptions(rawValue: 0))//Data.Base64DecodingOptions.ignoreUnknownCharacters
//                let contentStr = NSString.init(data: data! as Data, encoding: String.Encoding.utf8.rawValue)
                
//                let ssCC = "　　青鸾峰上，正是盛夏时节，峰顶一片碧绿，绿树成荫、鸟语蝉鸣，……"
//                let dataCC = ssCC .data(using: String.Encoding.utf8)
//                let base64CCStr = dataCC?.base64EncodedString()
//                print(base64CCStr ?? " ")
                

//                let base64CCStr = String.init(utf8String: content!)
//                //解码
//                let dataHH = NSData.init(base64Encoded: base64CCStr!, options: NSData.Base64DecodingOptions(rawValue: 0))//Data.Base64DecodingOptions.ignoreUnknownCharacters
//                let contentStrCD = String.init(data: dataHH! as Data, encoding: String.Encoding.utf8)
//                print(contentStrCD)
                
            }
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}
